package huriezy.quickcontacts.models.managers;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.content.Context;
import android.database.Cursor;
import android.os.Binder;
import android.os.IBinder;
import android.provider.CallLog;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.EditText;

public class ContactPhoneCallListener extends PhoneStateListener {

	private final EditText	phoneEditText;

	public ContactPhoneCallListener(EditText phoneEditText) {
		this.phoneEditText = phoneEditText;
	}

	@Override
	public void onCallStateChanged(int state, String incomingNumber) {

		if (state == TelephonyManager.CALL_STATE_RINGING) {

			Log.i(getClass().getSimpleName(), "Receiving new call...");

			try {
				if (incomingNumber != null) {
					Log.i(getClass().getSimpleName(), "Phone number : " + incomingNumber);
					phoneEditText.setText(incomingNumber);
				}

				disconnectPhone();

				Log.i(getClass().getSimpleName(), "End call.");

			}
			catch (Exception e) {
				Log.e(getClass().getSimpleName(), "Error while trying to catch phone call", e);
			}

		}

		super.onCallStateChanged(state, incomingNumber);
	}

	/**
	 * Return the last call registered in the CallLog system.
	 * 
	 * @param parentContext
	 * @return
	 */
	public static String getLastCall(Context parentContext) {
		String result = null;

		String[] fields = { CallLog.Calls.NUMBER, CallLog.Calls.TYPE };

		// most recent first
		String order = android.provider.CallLog.Calls.DATE + " DESC";

		Cursor cursor = parentContext.getContentResolver().query(android.provider.CallLog.Calls.CONTENT_URI, fields,
				null, null, order);

		cursor.moveToFirst();
		result = cursor.getString(0);

		return result;
	}

	private void disconnectPhone() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException,
			IllegalArgumentException, InvocationTargetException {
		String serviceManagerName = "android.os.ServiceManager";
		String serviceManagerNativeName = "android.os.ServiceManagerNative";
		String telephonyName = "com.android.internal.telephony.ITelephony";

		Class<?> telephonyClass;
		Class<?> telephonyStubClass;
		Class<?> serviceManagerClass;
		Class<?> serviceManagerNativeClass;

		Method telephonyEndCall;

		// Method getService;
		Object telephonyObject;
		Object serviceManagerObject;

		telephonyClass = Class.forName(telephonyName);
		telephonyStubClass = telephonyClass.getClasses()[0];
		serviceManagerClass = Class.forName(serviceManagerName);
		serviceManagerNativeClass = Class.forName(serviceManagerNativeName);

		Method getService = serviceManagerClass.getMethod("getService", String.class);

		Method tempInterfaceMethod = serviceManagerNativeClass.getMethod("asInterface", IBinder.class);

		Binder tmpBinder = new Binder();
		tmpBinder.attachInterface(null, "fake");

		serviceManagerObject = tempInterfaceMethod.invoke(null, tmpBinder);
		IBinder retbinder = (IBinder) getService.invoke(serviceManagerObject, "phone");
		Method serviceMethod = telephonyStubClass.getMethod("asInterface", IBinder.class);

		telephonyObject = serviceMethod.invoke(null, retbinder);
		telephonyEndCall = telephonyClass.getMethod("endCall");

		telephonyEndCall.invoke(telephonyObject);
	}
}
